<?php
class RateableField extends AppModel
{
	var $name = 'RateableField';
	
	var $validate = array('name'=>
							array(
								'required'=>true,
								'rule'=>VALID_NOT_EMPTY,
								'message'=>'This field is required.')
					);
		
	var $belongsTo = array(
						'ListingType'=>array(
							'className'=>'ListingType',
							'foreignKey'=>'listing_type_id'
							)
					);
	
					
	var $fieldProperties = array(
							'id'=>array(
								'type'=>'hidden',
								'group'=>array('add','edit')
								),
							'name'=>array(
								'type'=>'text',
								'label'=>'Name',
								'group'=>array('listing_type_add','edit'),
								'tooltip' => 'Name of your rateable field'
								),	
							'is_required'=>array(
								'type'=>'select',
								'label'=>'Required',
								'options' => array('No','Yes'),
								'group'=>array('listing_type_add','edit'),
								'tooltip' => 'Specify if its a mandatory field while user is posting a new review',
								),
							'weight'=>array(
								'type'=>'text',
								'label'=>'Weightage',
								'group'=>array('listing_type_add','edit'),
								'tooltip' => 'Specify weightage of this field'
								),
							'tooltip'=>array(
								'type'=>'text',
								'label'=>'Tooltip',
								'group'=>array('listing_type_add','edit'),
								'tooltip' => 'Enter tooltip text here which will be visible to user while posting a review'
								),
						);
	var $additionalFields = array('customFieldName');
	public function getRatableFieldsAsCustomFields($listingTypeId)
	{
		if(!class_exists('Review'))
		{
			App::import("Model",'Review');
		}
		$customFields = array();
		$ratableFields = $this->find('all',array('conditions'=>array('listing_type_id'=>$listingTypeId),'recursive'=>-1));
		foreach($ratableFields as $ratableField)
		{
			$name = $ratableField[$this->alias]['customFieldName'];	
			$customFields[$name]['required'] =  $ratableField[$this->alias]['is_required'];
			$customFields[$name]['label'] = $ratableField[$this->alias]['name'];
			$customFields[$name]['tooltip'] =  $ratableField[$this->alias]['tooltip'];
			$customFields[$name]['weight'] = $ratableField[$this->alias]['weight'];
			//temporary.
			$customFields[$name]['type'] = 'rating';
			$customFields[$name]['range'] = Review::RATING_MAX_VALUE; 
			$customFields[$name]['group'] = array('add','edit','index');
		}
		return $customFields;	
	}
	
	function customFieldName($record)
	{
      $recordId = $record;
      if(is_array($record))
      {
          $recordId = $record[$this->alias]['id'];
      }
		return 'rateablefield_rate_'.$recordId;
	}
	
	function initialize()
	{
		parent::initialize();
		//applying __().
		foreach($this->fieldProperties["is_required"]['options'] as $index=>$value)
		{
			$optinos[$index] = __($value);
		}
		$this->setFieldProperties('is_required',array('options'=>$optinos));
	}
	
}
?>